<?php
/**
 * Introduction: 分类逻辑处理
 * @author: 杨陈鹏
 * @date: 2016/4/08 03:04
 * @email: yangchenpeng@cdlinglu.com
 */
namespace Common\Logic;

use Common\Model;

class CategoryLogic
{

    static public $model;

    public function __construct()
    {

        self::$model = new Model\CategoryModel();

    }

    /**
     * @introduction: 获取数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param string $where 查询条件
     * @param string $limit 分页
     * @param string $fileds 需要获取的字段
     * @return array
     */
    static public function getData($where = '', $limit = '0,20', $fields = '*')
    {

        $data = self::$model
            ->where($where)
            ->order('sort asc')
            ->limit($limit)
            ->field($fields)
            ->select();

        $total = self::$model
            ->where($where)
            ->count();

        return ['data' => $data, 'total' => $total];

    }

    /**
     * @introduction: 获取一条数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array or string $where 查询条件
     * @param $fileds 需要获取的字段
     * @return array
     */
    static public function getOneData($where = '', $fileds = '*')
    {

        return self::$model
            ->where($where)
            ->field($fileds)
            ->find();

    }

    /**
     * Introduction: 添加分类
     * @author: 杨陈鹏
     * @date: 2016/4/08 03:04
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function create($data)
    {

        if (!$data['name'])
            return ['code' => 300, 'msg' => '分类名称不能为空'];

        if (!self::$model->add($data))
            return ['code' => 300, 'msg' => '添加失败!'];

        if ($data['pid'])
            self::$model
                ->where(['id' => $data['pid']])
                ->save(['child' => 1]);

        return ['code' => 200, 'msg' => '添加成功'];

    }

    /**
     * Introduction: 修改分类
     * @author: 杨陈鹏
     * @date: 2016/4/08 03:04
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function update($data)
    {

        if (!$data['id'])
            return ['code' => 300, 'msg' => '参数错误!'];

        $id = $data['id'];

        unset($data['id']);

        //检测新的父级是否是当前分类的子级  如果是,禁止修改
        $children = self::getChilds($data['id'], 'id,name');

        $children_ids = [];
        foreach ($children as $v) {

            $children_ids[] = $v['id'];

        }

        if (in_array($data['id'], $children_ids))
            return ['code' => 300, 'msg' => '不能选择当前分类的子级为父级'];


        if (!self::$model->where(['id' => $id])->save($data))
            return ['code' => 300, 'msg' => '修改失败'];


        //检测当前父级ID是否是当前ID或者当前ID的子级 如果不是则更新子父级关系
        if ($data['opid'] != $data['pid']) {


            //检测原父级下是否要有子级
            if (!self::$model->where(['pid' => $data['opid']])->count())

                self::$model
                    ->where([['id' => $data['opid']]])
                    ->save(['child' => 0]);

            //更新新的父级数据
            self::$model
                ->where(['id' => $data['pid']])
                ->save(['child' => 1]);

        }

        return ['code' => 200, 'msg' => '修改成功', 'data' => ['pid' => $data['pid']]];

    }


    /**
     * Introduction: 获取指定分类的所有子分类
     * @author: 杨陈鹏
     * @date: 2016/4/08 03:04
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getChilds($id = 0, $fields = '*')
    {
        //如果没指定ID  默认获取全部分类
        if (!$id) {

            $childs = self::$model
                ->field($fields)
                ->select();

            return $childs;

        } else {

            $childs = self::$model
                ->where(['pid' => $id])
                ->field($fields)
                ->select();

            if (!empty($childs)) {
                foreach ($childs as $v) {

                    $child = self::getChilds($v['id'], $fields);
                    if ($child) {

                        foreach ($child as $v)
                            array_push($childs, $v);

                    }

                }
                return $childs;
            }
        }

    }

    /**
     * Introduction: 获取指定分类的所有子分类
     * @author: 杨陈鹏
     * @date: 2016/4/08 03:04
     * @email: yangchenpeng@cdlinglu.com
     * @param: $id int 分类ID
     */
    static public function delete($id)
    {

        //检测当前分类是否有子分类如果有,禁止删除
        if (self::$model->where(['pid' => $id])->count())
            return ['code' => 300, '当前分类有子分类,禁止删除'];


        //检测当前分类及当前分类的子分类下是否有商品,如果有  禁止删除(待定)


        //获取当前分类的父分类
        $pid = self::$model->where(['id' => $id])->getField('pid');

        //删除当前ID
        if (!self::$model->where(['id' => $id])->delete())
            return ['code' => 300, '删除失败!'];

        //检测是否还有子分类
        if (!self::$model->where(['pid' => $pid])->count())
            self::$model->where(['id' => $pid])->save(['child' => 0]);

        return ['code' => 200, 'msg' => '删除成功!'];

    }

}